#AI file:
#Contains all AI fxns


import random
from Grid import *


#Dictionary to hold move positions and value of that move




def MakeMove(self):

    global MoveList
    
    MoveList={}

    #Tuple containing position values of possible moves
    PosValue=((3, 2, 3), (2, 4, 2), (3, 2, 3))

    #Search for open slots
    for y in range(3):
        for x in range(3):

            #If an empty slot is found, create a dictionary entry with position as key
            if self.Table[y][x] == ' ':

                MoveList[y, x]=PosValue[y][x]

                #If open position is in a diagonal position, then check diagonal move value
                if (x == 0 and y == 0) or (x == 2 and y == 0) or (x == 0 and y ==2) or (x == 2 and y == 2) or (x == 1 and y == 1):

                    CheckDiag(self, y, x)
                    
                #Check row and column value for all open positions
                CheckRow(self, y, x)
                CheckCol(self, y, x)

    #Return to fxn call the move chosen for the comp
    return ChooseMove(self)










#Figure out how many X & Os are in the diagonal with open slot
def CheckDiag(self, y, x):

    #Initially set X & O total to zero
    X=0
    O=0

    #If open slot is in upper-right to lower-left diagonal, figure out number of Xs & Os accordingly
    if (y == 0 and x == 0) or (y == 2 and x == 2) or (x == 1 and y == 1):

        for cnt in range(3):

            if self.Table[cnt][cnt] == 'X':
                
                X = X + 1



            if self.Table[cnt][cnt] == 'O':
                
                O = O + 1

        #Pass position of open slot (so move value can be added MoveList) and X & O totals in diagonal (to calc strategic value of move) 
        CalcMove(self, y, x, X, O)


    #If open slot is in upper-left to lower-right diagonal, figure out number of Xs & Os accordingly
    if (y == 0 and x == 2) or (y == 1 and x == 1) or (y == 2 and x == 0):

        for cnt in range(3):

            if self.Table[cnt][2-cnt] == 'X':

                X = X + 1


            if self.Table[cnt][2-cnt] == 'O':

                O = O + 1

        #Pass position of open slot (so move value can be added MoveList) and X & O totals in diagonal (to calc strategic value of move) 
        CalcMove(self, y, x, X, O)

                






#Figure out how many Xs and Os are in row with open slot
def CheckRow(self, y, x):

    #Initially zero out total Xs & Os in row
    X=0
    O=0

    #Check each position in row for Xs and Os
    for cnt in range(3):

        #If there is an X, add to X total
        if self.Table[y][cnt] == 'X':

            X = X + 1


        #If there is an O, add to O total
        if self.Table[y][cnt] == 'O':

            O = O + 1


    #Pass position of open slot (so move value can be added MoveList) and X & O totals in row (to calc strategic value of move) 
    CalcMove(self, y, x, X, O)

            









#Figure out how many Xs and Os are in column with open slot
def CheckCol(self, y, x):

    #Initially zero out total Xs & Os in column
    X=0
    O=0

    #Check each position in column for Xs and Os
    for cnt in range(3):

        #If there is an X, add to X total
        if self.Table[cnt][x] == 'X':

            X = X + 1


        #If there is an O, add to O total
        if self.Table[cnt][x] == 'O':

            O = O + 1


    #Pass position of open slot (so move value can be added MoveList) and X & O totals in Column (to calc strategic value of move) 
    CalcMove(self, y, x, X, O)








#Calculate possible move's strategic value
def CalcMove(self, y, x, X, O):

    #If 2 Xs are aligned add 2000 if Comp is X, else add 500
    if X == 2:

        if self.Comp == 'X':

            Add = 2000

        else:

            Add = 500



    #If 2 Os are aligned add 2000 if Comp is O, else add 500
    elif O == 2:

        if self.Comp == 'O':

            Add = 2000

        else:

            Add = 500



    #If there is only 1 X and the Comp is X add 100, else add 50
    elif X == 1 and O == 0:

        if self.Comp == 'X':

            Add = 100

        else:

            Add = 50



    #If there is only 1 O and the Comp is O add 100, else add 50
    elif X == 0 and O == 1:

        if self.Comp == 'O':

            Add = 100

        else:

            Add = 50



    #Else there are no Xs & Os aligned, so move has no strategic value
    else:

        Add = 0



    #Add move value to total value for position
    Temp=MoveList[y, x]

    Temp = Temp + Add

    MoveList[y, x] = Temp











#Makes final decision as to which open slot will be moved upon
def ChooseMove(self):

    #Create list of move values and sort it
    MoveValues = MoveList.values()

    MoveValues.sort()

    

    #Find out total number of moves
    Pos = len(MoveValues)

    ##print 'Diff=%s' % (self.Diff)

    ##print 'Original Pos=%s' % (Pos)

    #Figure out how far to slice
    if self.Diff == 1:

        Pos=0

    elif self.Diff == 2:

        Pos=Pos/2

    elif self.Diff == 3:

        Pos=Pos*(1.5)

    #Pull back one value in case 
    Pos=int(Pos-1)

    if Pos < 0:

        Pos=0

    ##print 'New Pos=%s' % (Pos)

    #Randomly select move
    Value=random.choice(MoveValues[Pos:])
    

    ##Value = MoveValues[Pos-1]


    #Reiterate through MoveList to find corresponding key for chosen move
    MoveKeys = MoveList.keys()

    for PosKey in MoveKeys:

        if MoveList[PosKey] == Value:

            KeyMove = PosKey

    #Return individual x & y positions for move
    y = KeyMove[0]
    x = KeyMove[1]

    return y, x
    
